from google.appengine.ext import db
from django.conf import settings
from django.template import TemplateDoesNotExist


from gaedbtemplates.models import Template


    
def load_template_source(template_name, template_dirs=None):
    """
    Tries to load the template from DBTEMPLATES_CACHE_DIR. If it does not exist 
    loads templates from the database by querying the database field ``name``
    with a template path and ``sites`` with the current site,
    and tries to save the template as DBTEMPLATES_CACHE_DIR/``name`` for subsequent
    requests.
    If DBTEMPLATES_CACHE_DIR is not configured falls back to database-only operation.
    """
    
    try:
#        print template_name
        query = Template.all()
        query.filter('name = ', template_name)
        t = query.get() 
        
        return (t.content, 'db:%s:%s' % (settings.DATABASE_ENGINE, template_name))
    except:
        print("aiaci")
        raise TemplateDoesNotExist, template_name
load_template_source.is_usable = True

